﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Mimoza.GUI;

namespace Mimoza.SystemPlugin
{
    public partial class PasswordPage : Mimoza.GUI.WizardPage
    {
        public PasswordPage()
        {
            InitializeComponent();
        }

        SystemPluginData m_data;

        override public void Init()
        {
            m_data = this.Wizard.UserData as SystemPluginData;

            textBoxPassword.Text = m_data.Password;
            textBoxConfirmPass.Text = m_data.Password;
            CheckPasswords();
            checkBoxAtPower.Checked = m_data.HasProtectionOnStartup;
            checkBoxScreenSaver.Checked = m_data.HasProtectionOnScreenSaver;

            this.checkBoxScreenSaver.CheckedChanged += new System.EventHandler(this.checkBoxScreenSaver_CheckedChanged);
            this.checkBoxAtPower.CheckedChanged += new System.EventHandler(this.checkBoxScreenSaver_CheckedChanged);
            this.textBoxConfirmPass.TextChanged += new System.EventHandler(this.textBoxConfirmPass_TextChanged);
            this.textBoxPassword.TextChanged += new System.EventHandler(this.textBoxPassword_TextChanged);
        }

        override public bool Try2LeavePage(WizardPageLeavingDirection direction)
        {
            if (direction == WizardPageLeavingDirection.Back)
            {
                return true;
            }

            if (!string.IsNullOrEmpty(textBoxPassword.Text) && textBoxPassword.Text == textBoxConfirmPass.Text)
            {
                m_data.Password = textBoxPassword.Text;
                m_data.HasProtectionOnStartup = checkBoxAtPower.Checked;
                m_data.HasProtectionOnScreenSaver = checkBoxScreenSaver.Checked;
            }
            else
            {
                m_data.Password = "";
                m_data.HasProtectionOnStartup = false;
                m_data.HasProtectionOnScreenSaver = false;
            }

            return true;
        }

        void CheckPasswords()
        {
            bool bEnabled = true;
            if (string.IsNullOrEmpty(textBoxPassword.Text) || textBoxPassword.Text != textBoxConfirmPass.Text)
            {
                checkBoxAtPower.Checked = false;
                checkBoxScreenSaver.Checked = false;
                bEnabled = false;
            }

            checkBoxAtPower.Enabled = bEnabled;
            checkBoxScreenSaver.Enabled = bEnabled;
        }

        private void textBoxPassword_TextChanged(object sender, EventArgs e)
        {
            CheckPasswords();
            this.RaiseOnPageModified();
        }

        private void textBoxConfirmPass_TextChanged(object sender, EventArgs e)
        {
            CheckPasswords();
            this.RaiseOnPageModified();
        }
        private void checkBoxScreenSaver_CheckedChanged(object sender, EventArgs e)
        {
            this.RaiseOnPageModified();
        }
    }
}
